package cn.tedu.springmvc2.controller;

import cn.tedu.springmvc2.mapper.UserMapper;
import cn.tedu.springmvc2.pojo.dto.UserDTO;
import cn.tedu.springmvc2.pojo.entity.User;
import cn.tedu.springmvc2.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

//@Controller
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @RequestMapping(value = "/users/reg",method = RequestMethod.POST)
//    @ResponseBody
    public String reg(UserDTO userDTO){
        System.out.println(userDTO);
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        user.setCreated(new Date());
        System.out.println(user);
        int num = userMapper.insert(user);
        return num > 0 ?"注册成功":"注册失败";
    }
    @RequestMapping(value = "/users/reg",method = RequestMethod.GET)
    public String insert(UserDTO userDTO){
        System.out.println("!!!!!!!!");
        return "注册成功";
    }
    @RequestMapping("/users/delete")
//    @ResponseBody
    public String delete(int id){
        System.out.println("删除用户id为："+id+"的记录");
        int num = userMapper.deleteById(id);
        return num > 0 ? "删除成功":"删除失败";
    }
    @RequestMapping("/users/update")
//    @ResponseBody
    public String update(UserDTO userDTO){
        System.out.println(userDTO);
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        int num = userMapper.updateByUsername(user);
        System.out.println(user);
        return num > 0 ?"修改成功":"修改失败";
    }
    @RequestMapping("/users/findAll")
    public List<UserVO> findAll(){
        return userMapper.findAll();
    }
}
